using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace StarSkipGame
{
    /// <summary>
    /// 玩家飞船类
    /// </summary>
    public class Player : GameObject
    {
        // 飞船颜色
        private readonly Color shipColor = Color.LightBlue;
        private readonly Color engineColor = Color.Orange;
        
        // 移动速度
        public float MoveSpeed { get; set; }
        
        public Player(float x, float y) : base(x, y, 30, 30)
        {
            // 飞船初始位置在屏幕中央
            MoveSpeed = 200.0f; // 像素/秒
        }
        
        // 输入状态
        public bool MoveUp { get; set; }
        public bool MoveDown { get; set; }
        public bool MoveLeft { get; set; }
        public bool MoveRight { get; set; }
        
        /// <summary>
        /// 更新玩家状态
        /// </summary>
        /// <param name="deltaTime">时间间隔</param>
        public override void Update(float deltaTime)
        {
            if (!IsActive) return;
            
            // 根据输入更新速度
            VelocityX = 0;
            VelocityY = 0;
            
            if (MoveUp) VelocityY = -MoveSpeed;
            if (MoveDown) VelocityY = MoveSpeed;
            if (MoveLeft) VelocityX = -MoveSpeed;
            if (MoveRight) VelocityX = MoveSpeed;
            
            // 对角线移动时保持相同速度
            if (MoveUp && MoveDown) VelocityY = 0;
            if (MoveLeft && MoveRight) VelocityX = 0;
            
            // 调用基类更新方法
            base.Update(deltaTime);
        }
        
        /// <summary>
        /// 绘制飞船
        /// </summary>
        /// <param name="g">绘图对象</param>
        public override void Draw(Graphics g)
        {
            if (!IsActive) return;
            
            // 设置高质量绘图
            g.SmoothingMode = SmoothingMode.AntiAlias;
            
            // 使用当前选择的飞船皮肤
            var currentSkin = ShipSkinManager.Instance.GetCurrentSkin();
            currentSkin.Draw(g, X, Y, Width, Height);
            
            // 绘制引擎火焰效果（如果正在移动）
            if (MoveUp || MoveDown || MoveLeft || MoveRight)
            {
                float centerX = X + Width / 2.0f;
                PointF[] enginePoints = {
                    new PointF(centerX - 5, Y + Height),       // 左引擎
                    new PointF(centerX - 2, Y + Height + 8),
                    new PointF(centerX + 2, Y + Height + 8),
                    new PointF(centerX + 5, Y + Height)        // 右引擎
                };
                
                using (SolidBrush engineBrush = new SolidBrush(engineColor))
                {
                    g.FillPolygon(engineBrush, enginePoints);
                }
            }
        }
        
        /// <summary>
        /// 检查边界碰撞，防止飞船移出屏幕
        /// </summary>
        /// <param name="screenWidth">屏幕宽度</param>
        /// <param name="screenHeight">屏幕高度</param>
        public void CheckBounds(int screenWidth, int screenHeight)
        {
            // 左边界
            if (X < 0) X = 0;
            
            // 右边界
            if (X + Width > screenWidth) X = screenWidth - Width;
            
            // 上边界
            if (Y < 0) Y = 0;
            
            // 下边界
            if (Y + Height > screenHeight) Y = screenHeight - Height;
        }
        
        /// <summary>
        /// 重置玩家状态
        /// </summary>
        /// <param name="x">新的X坐标</param>
        /// <param name="y">新的Y坐标</param>
        public void Reset(float x, float y)
        {
            X = x;
            Y = y;
            VelocityX = 0;
            VelocityY = 0;
            IsActive = true;
            
            // 重置输入状态
            MoveUp = false;
            MoveDown = false;
            MoveLeft = false;
            MoveRight = false;
        }
        
        /// <summary>
        /// 设置移动速度倍数
        /// </summary>
        /// <param name="multiplier">速度倍数</param>
        public void SetSpeedMultiplier(float multiplier)
        {
            MoveSpeed = 200.0f * multiplier;
        }
    }
} 